﻿Imports System.Windows.Forms
Imports System.Net.Sockets

Public Class MDIParent1

    Dim s As Socket
    Dim vdos As System.Collections.Generic.Dictionary(Of Integer, vdoConsoleForm)

    Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
        ' Create a new instance of the child form.
        Dim ChildForm As New System.Windows.Forms.Form
        ' Make it a child of this MDI form before showing it.
        ChildForm.MdiParent = Me
        m_ChildFormNumber += 1
        ChildForm.Text = "Window " & m_ChildFormNumber
        ChildForm.Show()
    End Sub

    Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
        s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
        s.ReceiveBufferSize = 4 * 1024 * 1024
        s.Connect("127.0.0.1", 4321)
        Dim p As CPacket = New CPacket
        p.SetISetPacketTypeD(1)
        p.Pack(1)

        p.Transmit(s)
        vdos = New Collections.Generic.Dictionary(Of Integer, vdoConsoleForm)
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveAsToolStripMenuItem.Click
       
    End Sub


    Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CutToolStripMenuItem.Click
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CopyToolStripMenuItem.Click
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PasteToolStripMenuItem.Click
        'Use My.Computer.Clipboard.GetText() or My.Computer.Clipboard.GetData to retrieve information from the clipboard.
    End Sub

    Private Sub ToolBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ToolBarToolStripMenuItem.Click
        Me.ToolStrip.Visible = Me.ToolBarToolStripMenuItem.Checked
    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StatusBarToolStripMenuItem.Click
        Me.StatusStrip.Visible = Me.StatusBarToolStripMenuItem.Checked
    End Sub

    Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CascadeToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub TileVerticalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileVerticalToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.TileVertical)
    End Sub

    Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileHorizontalToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ArrangeIconsToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub

    Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CloseAllToolStripMenuItem.Click
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub

    Private m_ChildFormNumber As Integer

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            While s.Available >= 10
                If s.Available >= 10 Then

                    Dim id(1) As Byte
                    Dim sid(1) As Byte

                    Dim target(4) As Byte
                    Dim size(4) As Byte

                    s.Receive(id, 1, SocketFlags.None)
                    s.Receive(sid, 1, SocketFlags.None)
                    s.Receive(target, 4, SocketFlags.None)
                    s.Receive(size, 4, SocketFlags.None)

                    Dim isize As Integer = BitConverter.ToInt32(size, 0)

                    Dim data(isize) As Byte

                    If isize > 0 Then
                        s.Receive(data, isize, SocketFlags.None)
                    End If

                    Dim p As CPacket = New CPacket()

                    If isize > 0 Then
                        p.Pack(data, isize)
                    End If

                    p.SetISetPacketTypeD(id(0))
                    p.SetSubPacketType(sid(0))
                    p.targetProcess = BitConverter.ToInt32(target, 0)

                    Select Case p.GetID
                        Case 1 ' authentication
                        Case 2
                        Case 3
                        Case 4
                        Case 5 'vdo open

                            Select Case p.GetSubPacketType
                                Case 1 ' cdo console
                                    Dim v As New vdoConsoleForm
                                    v.id = p.targetProcess
                                    vdos.Add(p.targetProcess, v)
                                    v.MdiParent = Me
                                    v.Show()
                                    v.handlePacket(p)
                            End Select

                        Case 6

                            Select Case p.GetSubPacketType
                                Case 1 '// line out
                                    Dim v As vdoConsoleForm
                                    v = vdos(p.targetProcess)
                                    v.handlePacket(p)
                            End Select

                    End Select

                End If

            End While
        Catch ex As Exception

        End Try
    End Sub
End Class
